package com.nforce.linremote;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ListFavouritesActivity extends ListActivity implements
	OnItemClickListener {

    private LayoutInflater mInflater;
    private Vector<String> data;
    private CustomAdapter mAdapter;
    private SharedPreferences mPrefs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_list_favourites);
	getActionBar().setDisplayHomeAsUpEnabled(true);

	mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
	data = new Vector<String>();

	mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
	Set<String> set = mPrefs.getStringSet(Constants.KEY_PREF_FAVS, new HashSet<String>());

	for (String s : set) {
	    data.add(s);
	}

	mAdapter = new CustomAdapter(this, R.layout.list_favourites_row, R.id.ipView, data);
	setListAdapter(mAdapter);
	getListView().setOnItemClickListener(this);

	findViewById(R.id.add_fav_button).setOnClickListener(new View.OnClickListener() {
	    @Override
	    public void onClick(View view) {
		AsyncTask<Void, Void, Void> sendJob = new AsyncTask<Void, Void, Void>() {

		    @Override
		    protected Void doInBackground(Void... params) {
			try {

			    data.add(BlockingDialog.askArg(ListFavouritesActivity.this, null, ""));
			} catch (InterruptedException e) {
			}
			return null;
		    }

		    @Override
		    protected void onPostExecute(Void result) {
			mAdapter.notifyDataSetChanged();
		    }
		};
		sendJob.execute((Void) null);
	    }
	});
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, final int pos, long id) {
	AsyncTask<Void, Void, Void> sendJob = new AsyncTask<Void, Void, Void>() {

	    String ip;

	    @Override
	    protected Void doInBackground(Void... params) {
		try {
		    ip = BlockingDialog.askArg(ListFavouritesActivity.this, null, mAdapter.getItem(pos));
		} catch (InterruptedException e) {
		}
		return null;
	    }

	    @Override
	    protected void onPostExecute(Void result) {
		data.set(pos, ip);
		mAdapter.notifyDataSetChanged();
	    }
	};
	sendJob.execute((Void) null);
    }

    @SuppressLint("CommitPrefEdits")
    private void save() {
	Set<String> set = new HashSet<String>();
	for (int i = 0; i < mAdapter.getCount(); i++) {
	    set.add(mAdapter.getItem(i));
	}
	Editor editor = mPrefs.edit();
	editor.putStringSet(Constants.KEY_PREF_FAVS, set);
	editor.commit();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
	switch (item.getItemId()) {
	    case android.R.id.home:
		NavUtils.navigateUpFromSameTask(this);
		return true;
	}
	return super.onOptionsItemSelected(item);
    }

    private class CustomAdapter extends ArrayAdapter<String> {

	public CustomAdapter(Context context, int resource, int textViewResourceId, List<String> objects) {
	    super(context, resource, textViewResourceId, objects);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
	    ViewHolder holder = null;

	    TextView item = null;

	    final String rowData = getItem(position);

	    if (null == convertView) {
		convertView = mInflater.inflate(R.layout.list_favourites_row, null);
		holder = new ViewHolder(convertView);
		convertView.setTag(holder);
	    }

	    convertView.findViewById(R.id.delete_fav_button).setOnClickListener(new View.OnClickListener() {

		@Override
		public void onClick(View v) {
		    mAdapter.remove(rowData);
		    mAdapter.notifyDataSetChanged();
		}
	    });

	    holder = (ViewHolder) convertView.getTag();
	    item = holder.getItem();
	    item.setText(rowData);

	    return convertView;
	}

	@Override
	public void notifyDataSetChanged() {
	    save();
	    super.notifyDataSetChanged();
	}
    }

    private class ViewHolder {
	private final View mRow;
	private TextView item = null;

	public ViewHolder(View row) {
	    mRow = row;
	}

	public TextView getItem() {
	    if (null == item) {
		item = (TextView) mRow.findViewById(R.id.ipView);
	    }
	    return item;
	}
    }

}
